﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using uForum.Businesslogic;

namespace uForum.usercontrols
{
    public partial class DBInstaller : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (uForum.Businesslogic.Data.SqlHelper.Utility.GetType().ToString() == "umbraco.DataLayer.SqlHelpers.SqlServer.SqlServerUtility")
            {
                WrongDbPanel.Visible = false;
            }
            else
            {
                StartPanel.Visible = false;
            } 
            DonePanel.Visible = false;
        }

        protected void installButton_Click(object sender, EventArgs e)
        {

            //Do DB stuff
            #region sqlstring
            string sql = @"
/****** Object:  Table [dbo].[forumForums]    Script Date: 06/22/2009 14:49:10 ******/
SET ANSI_NULLS ON
;
SET QUOTED_IDENTIFIER ON
;
CREATE TABLE [dbo].[forumForums](
	[id] [int] NOT NULL,
	[latestTopic] [int] NOT NULL CONSTRAINT [DF_forumForums_latestTopic]  DEFAULT ((0)),
	[latestComment] [int] NOT NULL CONSTRAINT [DF_forumForums_latestComment]  DEFAULT ((0)),
	[totalTopics] [int] NOT NULL CONSTRAINT [DF_forumForums_totalTopics]  DEFAULT ((0)),
	[totalComments] [int] NOT NULL CONSTRAINT [DF_forumForums_totalComments]  DEFAULT ((0)),
	[latestAuthor] [int] NOT NULL CONSTRAINT [DF_forumForums_latestAuthor]  DEFAULT ((0)),
	[latestPostDate] [datetime] NOT NULL CONSTRAINT [DF_forumForums_latestPostDate]  DEFAULT (getdate()),
	[sortOrder] [int] NOT NULL CONSTRAINT [DF_forumForums_sortOrder]  DEFAULT ((0)),
	[parentId] [int] NOT NULL,
 CONSTRAINT [PK_forumForums] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
;


/****** Object:  Table [dbo].[forumTopics]    Script Date: 06/22/2009 14:49:46 ******/
SET ANSI_NULLS ON
;
SET QUOTED_IDENTIFIER ON
;
CREATE TABLE [dbo].[forumTopics](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[parentId] [int] NOT NULL,
	[memberId] [int] NOT NULL,
	[title] [nvarchar](70) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[body] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[created] [datetime] NOT NULL CONSTRAINT [DF_forumTopics_created]  DEFAULT (getdate()),
	[updated] [datetime] NOT NULL CONSTRAINT [DF_forumTopics_updated]  DEFAULT (getdate()),
	[locked] [bit] NOT NULL CONSTRAINT [DF_forumTopics_locked]  DEFAULT ((0)),
	[latestReplyAuthor] [int] NOT NULL CONSTRAINT [DF_forumTopics_latestReplyAuthor]  DEFAULT ((0)),
	[urlName] [nvarchar](300) COLLATE Danish_Norwegian_CI_AS NOT NULL,
	[replies] [int] NOT NULL,
 CONSTRAINT [PK_forumTopics] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

;
ALTER TABLE [dbo].[forumTopics]  WITH CHECK ADD  CONSTRAINT [FK_forumTopics_umbracoNode1] FOREIGN KEY([parentId])
REFERENCES [dbo].[umbracoNode] ([id])
ON DELETE CASCADE
;
ALTER TABLE [dbo].[forumTopics] CHECK CONSTRAINT [FK_forumTopics_umbracoNode1]
;


/****** Object:  Table [dbo].[forumComments]    Script Date: 06/22/2009 14:51:18 ******/
SET ANSI_NULLS ON
;
SET QUOTED_IDENTIFIER ON
;
CREATE TABLE [dbo].[forumComments](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[topicId] [int] NOT NULL,
	[memberId] [int] NOT NULL,
	[body] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[created] [datetime] NOT NULL CONSTRAINT [DF_forumComments_created]  DEFAULT (getdate()),
	[position] [int] NOT NULL,
 CONSTRAINT [PK_forumComments] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

;
ALTER TABLE [dbo].[forumComments]  WITH CHECK ADD  CONSTRAINT [FK_forumComments_forumTopics] FOREIGN KEY([topicId])
REFERENCES [dbo].[forumTopics] ([id])
ON DELETE CASCADE
;
ALTER TABLE [dbo].[forumComments] CHECK CONSTRAINT [FK_forumComments_forumTopics]
;
            ";
            #endregion sqlstring

            try
            {
                Data.SqlHelper.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                completemessagelabel.Text = "The database failed to install properly. Errormessage: " + ex.ToString();    
            }

            StartPanel.Visible = false;
            DonePanel.Visible = true;
        }
    }
}